#include "actions.h"
#include <QAction>
#include "additems.h"
#include "graphicsview.h"

//Actions g_Actions;
Actions * gActions = NULL;//&g_Actions;

Actions::Actions(QObject *parent)
	: QObject(parent)
{
	createActions();
}

Actions::~Actions()
{

}

void Actions::createActions()
{
	
	mNew = new QAction(tr("New..."),this);
	mNew->setData(tr("New"));
	mNew->setIcon(QIcon(":/MindFirst/images/btn_add.gif"));

	mHelp = new QAction(tr("Help"),this);
	mHelp->setData(tr("Help"));
	mHelp->setIcon(QIcon(":/MindFirst/images/btn_help_top.gif"));

	mCurrentTool = Tools_e::eSelectTool;

	mSelectTool = new QAction(tr("Select"),this);
	mSelectTool->setData(tr("SelectTool"));
	mSelectTool->setIcon(QIcon(":/MindFirst/images/cursor_select_tool.png"));

	mRectTool = new QAction(tr("Rect"),this);
	mRectTool->setData(tr("RectTool"));
	mRectTool->setIcon(QIcon(":/MindFirst/images/node.gif"));

	mDrawRectByMouse = new QAction(tr("DrawRectByMouse"),this);
	mDrawRectByMouse->setData(tr("DrawRectByMouse"));

	mLineTool = new QAction(tr("Line"),this);
	mLineTool->setData(tr("LineTool"));
	mLineTool->setIcon(QIcon(":/MindFirst/images/link.gif"));

	mDrawLineByMouse = new QAction(tr("AddLineItem"),this);
	mDrawLineByMouse->setData(tr("AddLineItem"));
}
int Actions::initActions()
{
	connect(mSelectTool,SIGNAL(triggered()),this,SLOT(selectToolEvent()));
	connect(mRectTool,SIGNAL(triggered()),this,SLOT(selectToolEvent()));
	connect(mLineTool,SIGNAL(triggered()), this, SLOT(selectToolEvent()));

	connect(mDrawRectByMouse, SIGNAL(triggered()), gAddItems, SLOT(addItemEvent()));
	connect(mDrawLineByMouse,SIGNAL(triggered()),gAddItems,SLOT(addItemEvent()));

	return 0;
}
// event slots;
void Actions::selectToolEvent()
{
	QAction * action = qobject_cast<QAction*>(sender());
	if( !action )	return;
	//
	QString s = action->data().toString();
	if( s == tr("SelectTool"))
	{
		setCurrentTool(Tools_e::eSelectTool);
		gView->setCursor(Qt::ArrowCursor);
	}
	else if (s == tr("RectTool"))
	{
		setCurrentTool(Tools_e::eRectTool);
		gView->setCursor(Qt::CrossCursor);
	}
	else if(s == tr("LineTool"))
	{
		setCurrentTool(Tools_e::eLineTool);
		gView->setCursor(Qt::CrossCursor);
	}
	else if( s == tr("TextTool"))
	{
		setCurrentTool(Tools_e::eTextTool);
		gView->setCursor(Qt::ArrowCursor);
	}
	else
	{
		gView->setCursor(Qt::ArrowCursor);
	}
}